Page({
  data: {
    rows: 10,
    cols: 2,
    allMatrix: [],
    matrix: [],
    jumpNumber: 1
  },
  onLoad() {
    let allMatrix = wx.getStorageSync('allMatrix') || [];
    if (allMatrix.length === 0) {
      for (let i = 0; i < 100; i++) {
        let m = [];
        for (let r = 0; r < 10; r++) {
          let row = [];
          for (let c = 0; c < 2; c++) {
            row.push({ row: r, col: c, value: {} });
          }
          m.push(row);
        }
        allMatrix.push(m);
      }
      wx.setStorageSync('allMatrix', allMatrix);
    }
    this.setData({
      allMatrix,
      matrix: JSON.parse(JSON.stringify(allMatrix[0])),
      jumpNumber: 1
    });
  },
  onNumberInput(e) {
    let val = parseInt(e.detail.value) || 1;
    this.setData({ jumpNumber: val });
  },
  onRefreshMatrix() {
    this.refreshMatrix(this.data.jumpNumber);
  },
  refreshMatrix(group) {
    let idx = group - 1;
    let { allMatrix, rows, cols } = this.data;
    if (!allMatrix[idx]) {
      let m = [];
      for (let r = 0; r < rows; r++) {
        let row = [];
        for (let c = 0; c < cols; c++) {
          row.push({ row: r, col: c, value: {} });
        }
        m.push(row);
      }
      allMatrix[idx] = m;
      wx.setStorageSync('allMatrix', allMatrix);
    }
    this.setData({ 
      matrix: JSON.parse(JSON.stringify(allMatrix[idx])),
      jumpNumber: group
    });
  },
  goDetail(e) {
    const { row, col } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/detail/detail?row=${row}&col=${col}&group=${this.data.jumpNumber}`
    });
  }
}); 